//注册的表单验证

//手机号验证
var $str = null;
var fnTxt = false;
$("#txt1").focus(function(){
	$(".txt").css("border-color","#0083CE")
	$(".txt .pic2").css("display","none");
	$(".txt .pic22").css("display","none");
	$(".txt .pic21").css("display","none");
})
$("#txt1").blur(function(){
	$str = $("#txt1").val();
	var $pattern = /^(13|14|15|18)[0-9]{9}$/;
	var $val = $("#txt1").val();
	if (!($val == '')) {
		if ($pattern.test($str)) {
			$(".txt .pic21").css("display","block");
			$(".txt").css("border-color","#85c02e");
			$(".txt .pic2").css("display","none");
			$(".txt .pic22").css("display","none");
			fnTxt = true;
		}else {
			$(".txt .pic22").css({"display":"block","color":"#e72418"});
			$(".txt").css("border-color","#e72418");
			$(".txt .pic2").css("display","none");
			$(".txt .pic21").css("display","none");
			fnTxt = false;
		}
	}else{
		$(".txt .pic2").css("display","block");
		$(".txt").css("border-color","#fa0");
	}
})

//密码框验证
//密码框得焦事件
var fnPas = false;
$("#pas1").focus(function(){
	$(".pas .pic42").css("display","block");
	$(".pas").css("border-color","#0083CE");
	$(".pas .pic43").css({"display":"none"});
	$(".pas .pic44").css({"display":"none"});
})
//密码框失焦事件
$("#pas1").blur(function(){
	var $pat = /^.{6,20}$/;
	var val = $("#pas1").val();
	if (!(val == '')) {
		if (!$pat.test(val)) {
			$(".pas .pic44").css("display","block");
			$(".pas .pic43").css({"display":"none"});
			$(".pas ul").css("display","none");
			$(".pas .pic43").css({"display":"none"});
			$(".pas .pic42").css({"display":"none"});
		}else{
			$(".pas .pic4").css("display","block");
			$(".pas ul").css("display","none");
			fnPas = true;
		}
	}else{
		$(".pas .pic43").css({"display":"block","color":"#e72418"});	
		$(".pas").css("border-color","#e72418");
		$(".pas .pic42").css({"display":"none"});
		$(".pas ul").css("display","none");
	}
})
//密码框按下事件
$("#pas1").keyup(function(){
	//弱：纯数字，纯字母，纯特殊字符
	var weak = /^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+)$/;
	//中：字母+数字，字母+特殊字符，数字+特殊字符
	var oIn = /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/
	//强：字母+数字+特殊字符 
	var strong = /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/
	$(".pas ul").css("display","block");
	$(".pas .pic42").css({"display":"none"});
	var val = $("#pas1").val();
	if (weak.test(val)) {
		$(".pas ul li").eq(0).css("background-color","coral")
	}
	if (oIn.test(val)) {
		$(".pas ul li").eq(1).css("background-color","coral")
	}
	if (strong.test(val)) {
		$(".pas ul li").eq(2).css("background-color","coral")
	}
})

//确认密码
var fnPwd = false;
$(".confirm #con1").focus(function(){
	$(".confirm .pic61").css("display","block");
	$(".confirm .pic62").css("display","none");
	$(".confirm .pic6").css("display","none");
	$(".confirm").css("border-color","#0083CE");
})
$(".confirm #con1").blur(function(){
	if ($("#pas1").val() == $("#con1").val()) {
		$(".confirm .pic6").css("display","block");
		$(".confirm .pic62").css("display","none");
		$(".confirm .pic61").css("display","none");
		fnPwd = true;
	}else{
		$(".confirm .pic62").css("display","block");
		$(".confirm .pic61").css("display","none");
	}
})
//是否勾选协议
var fnCheck = false;

$("#che").click(function(){
	if ($("#che").is(':checked')) {
		$(".register_form p i").css("display","none");
		fnCheck = true;
	}else{
		$(".register_form p i").css("display","block");
	}
})


//提交注册
$(".register_form #btn").click(function(){
	if (fnTxt && fnPas && fnPwd && fnCheck) {
		var users = getCookie("registerUsers") ? getCookie("registerUsers") : "";
		users = convertStrToObj(users);
		if($("#txt1").val() in users){ //判断usn属性是否在users对象中。
			alert("用户名已经被注册");
			return;
		}else{
			users[$("#txt1").val()] = $("#pas1").val();
			console.log($("#pas1").val())
			//将用户信息对象转化回字符串，以便于设置cookie
			userStr = convertObjToStr(users);
			//设置用户信息cookie
			setCookie("registerUsers",userStr,7);
			console.log(decodeURIComponent(document.cookie))
//			alert("注册成功！");
			location.href = 'entry.html';
		}
	}
})
//将字符串转为对象
function convertStrToObj(str){
	if(!str){
		return {};
	}
	var users = str.split(":"); //将字符串转为数组 
	var obj = {};
	//遍历数组
	for(var i = 0; i < users.length; i ++){
		//将字符串转为数组
		var userData = users[i].split(",");
		//["test1",123] ["test2","abc"] ["test3",888]
		obj[userData[0]] = userData[1];
	}
	return obj;
}
//将对象转为字符串
function convertObjToStr(obj){
	////假设不为空："test1,123:test2,abc:test3,888:李涛,123"
	var str = "";
	for(var usn in obj){
		var pwd = obj[usn];
		if(str){
			//看是否是第一组用户名和密码，如果不是，先在前面添加一个：
			str += ":";
		}
		str += usn + ',' + pwd;
	}
	return str;
}





































